// pages/02admin/00waiter/waiter.js
var app = getApp()
var appData = app.globalData
Page({
    data: {
        waiterList:null,
        i:''
    },

    onLoad: function (options) {
        var that = this
        var param = { uid: appData.shopInfo.shopInfo.userid }
        app.getJsonData('api/admin/fuwu_list.php', param, function (waiterData) {
            that.handleWaiterData(waiterData)
        })
        //监听服务员设置的通知
        app.setNoticeWaiterList(function (data) {
            var i = that.data.i
            var list = that.data.waiterList
            if (i != '') {
                list[i] = data
            }else{
                list.push(data)
            }
            console.log('list',list)
            console.log('that.data.waiterList', that.data.waiterList)
            that.setData({
                waiterList:list
            })
        })
    },

    editWaiter:function(e){
        if (app.globalData.loginMode == 'boss'){
            let i = e.currentTarget.id
            this.data.i = i
            var url_end = ''
            if (i != '') {
                let oneWaiter = JSON.stringify(this.data.waiterList[i])
                url_end = '?waiter=' + oneWaiter
            }

            wx.navigateTo({
                url: '/pages/02admin/01setWaiter/setWaiter' + url_end
            })
        }else{
            wx.showToast({
                title: '需要掌柜登录才能操作',
            })
        }
    },

    delWaiter: function (e) {
        if (app.globalData.loginMode == 'boss'){
        let i = e.currentTarget.id
        var list = this.data.waiterList
        var param = {
            id: list[i].id,
            sql: 'delete from'
        }
        var that = this
        app.getJsonData('api/admin/waiter_save.php', param, function (res) {
            if (res.code == 0) {
                list.splice(i, 1)
                that.setData({
                    waiterList: list
                })
            } else {
                wx.showModal({
                    title: '删除失败！',
                    content: res.message
                })
            }
        })
        }
        else {
            wx.showToast({
                title: '需要掌柜登录才能操作',
            })
        }
    },

    handleWaiterData: function (waiterData){
        if(waiterData.code == 0){
            this.setData({
                waiterList: waiterData.data
            })
        }
        console.log(this.data.waiterList)
    },

    
})